<template>
  <FModal width="800" ref="refFModal" :header="modelTitle" @ok="ok">
    <div style="padding-right: 30px">
      <el-form ref="refForm" label-width="120px" :model="positionLevelData">
        <el-form-item prop="positionLevelName" label="职务级别" :rules="[required]">
          <el-input v-focus placeholder="请输入职务级别" v-model="positionLevelData.positionLevelName"/>
        </el-form-item>

        <el-form-item prop="positionLevelCode" label="职务级别编码" :rules="[required]">
          <el-input placeholder="请输入职务级别编码" v-model="positionLevelData.positionLevelCode"/>
        </el-form-item>

        <el-form-item prop="sort" label="职务级别序号" :rules="[required]">
          <el-input-number style="width: 100%" placeholder="请输入职务级别序号" v-model="positionLevelData.sort"/>
        </el-form-item>

        <el-form-item prop="positionLevelStatus" label="状态" :rules="[required]">
          <SystemDict dict-code="positionLevelStatus" placeholder="请选择状态"
                      v-model="positionLevelData.positionLevelStatus"/>
        </el-form-item>

        <el-form-item prop="positionLevelDesc" label="描述" :rules="[]">
          <el-input type="textarea" placeholder="请输入描述" v-model="positionLevelData.positionLevelDesc"/>
        </el-form-item>
      </el-form>
    </div>
  </FModal>
</template>
<script setup>
import FModal from '@/components/FModal/index.vue';
import {isEmpty, real, required} from '@/utils/CommonUtils';
import {computed, ref, shallowRef} from 'vue';
import SystemDict from '@/components/SystemDict/index.vue';
import {ElMessage} from 'element-plus';
import {getAction, postAction} from '@/request/BaseRequest';

const loading = shallowRef(false);
const refFModal = shallowRef();
const refForm = shallowRef();
const positionLevelData = ref({});
let unitData = undefined;
const emits = defineEmits(['reloadPositionLevel'])

function ok() {
  refForm.value.validate((valid, errors) => {
    if (valid) {
      doSave(real(positionLevelData));
    }
  });
}

function doSave(data) {
  loading.value = true;
  data.unitId = real(unitData).id;
  postAction('/sys/positionLevel/save', data).then(data => {
    loading.value = false;
    if (data.success) {
      refFModal.value.close();
      ElMessage({
        message: '保存成功',
        type: 'success',
      });
      emits('reloadPositionLevel');
    }
  });
}

function setData(_unitData, positionLevel) {
  unitData = real(_unitData);
  if (isEmpty(positionLevel)) {
    //新建
    createDefaultData();
  } else {
    //编辑
    positionLevel = JSON.parse(JSON.stringify(real(positionLevel)));
    loadPositionLevelData(positionLevel);
  }
  refFModal.value.open();
}

function loadPositionLevelData(positionLevel) {
  positionLevelData.value = positionLevel;
}

function createDefaultData() {
  positionLevelData.value = {
    unitId: unitData.id,
    positionLevelName: undefined,
    positionLevelStatus: '0',
    positionLevel: '0',
  };
  loadMaxPositionLevelSort();
}

function loadMaxPositionLevelSort() {
  loading.value = true;
  getAction('/sys/positionLevel/loadMaxPositionLevelSort').then(data => {
    loading.value = false;
    if (data.success) {
      positionLevelData.value.sort = data.result;
    }
  });
}

const modelTitle = computed(() => {
  let data = real(positionLevelData);
  if (isEmpty(data.id)) {
    return '新建职务级别';
  }
  return '编辑职务级别';
});

defineExpose({
  setData
});
</script>
<script>
export default {
  name: 'PositionLevelEditModal'
};
</script>
<style>

</style>
